<?php

namespace My\Controller;

use Utuo\BackgroundController;
use Utuo\PHPExcel;

class SeckillApplyController extends BackgroundController {
	public function _initialize() {
		$this->setControllerTitle ( "一元秒杀申请管理" );
		$this->setActionTitle ( array (
				'index' => '一元秒杀申请列表',
				'data' => '数据概况',
				// 'modify' => '修改一元秒杀',
				'recycle' => '回收站' 
		) );
		$this->setActionNav ( array (
				'index',
				'data',
				'recycle' 
		) );
	}
	public function getLists($is_deleted) {
		$condation = array (
				'is_deleted' => $is_deleted 
		);
		$date = I ( 'date' );
		
		$date and $condation ['date'] = $date and $params ['date'] = $date;
		
		$seckillApply = M ( 'SeckillApply' )->where ( $condation )->page ( $this->getPage (), $this->getPageSize () )->order ( 'id desc' )->select ();
		
		foreach ( $seckillApply as $k => &$v ) {
			$v ['statusOptions'] = \Utuo\Form::getOption ( D ( 'SeckillApply' )->status, $v ['status'] );
		}
		$total = M ( 'SeckillApply' )->where ( $condation )->count ();
		$this->showPage ( $total, $this->getPageSize (), array (
				'date' => $date 
		) );
		$this->assign ( "seckillApply", $seckillApply );
		$this->assign ( 'date', $date );
		$this->assign("params",$params);
		$this->display ();
	}
	// 设置审核状态
	public function setStatusAction() {
		$id = I ( "id", 0, "intval" );
		$status = I ( "status", 0, "intval" );
		$d = D ( "SeckillApply" );
		if ($d->where ( "id={$id}" )->setField ( "status", $status )) {
			$this->ajaxSuccess ();
		} else {
			$this->ajaxError ( $d->getError () );
		}
	}
	// 显示数据
	public function dataAction() {
		$seckill=M('Seckill')->where('status=1 and is_deleted=0')->find();
		$m = M ( 'SeckillApply' );
		$data = $m->field ( "date,max(apply_time) as maxtime,min(apply_time) as mintime,count(date) as num" )->group ( 'date' )->where ( 'is_error=0' )->select ();
		$partake = $m->field ( "date,count(date) as totalCount" )->group ( 'date' )->select ();
		foreach ( $data as &$v ) {
			foreach ( $partake as $p ) {
				if ($v ['date'] == $p ['date']) {
					$v ['totalCount'] = $p ['totalCount'];
				}
			}
			$v ['time'] = $v ['maxtime'] - strtotime($v['date'].$seckill ['start_time']);
			
			$hours = floor ( $v ['time'] / 3600 );
			$minutes = floor ( $v ['time'] % 3600 / 60 );
			$seconds = $v ['time'] % 3600 % 60;
			$v ['totalTime'] = $hours . "时" . $minutes . "分" . $seconds . "秒";
		}
		
		$this->assign ( "data", $data );
		$this->display ();
	}
	/**
	 * 导出Excel
	 */
	public function exportXLSAction() {
		$date = I('date');
		$params = array(
				'date' => strtotime($date),
				
		);
		
		$productOrderGoodsLists = $this->search( $params );
        $objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setCreator("微美风铃")
					->setLastModifiedBy("微美风铃")
					->setTitle("申请人员信息")
					->setSubject("申请人员信息")
					->setDescription("申请人员信息")
					->setKeywords("申请人员信息")
					->setCategory("申请人员信息");
	
	
		/*设置标题*/
		$titles = array('ID','姓名','电话','宿舍号','活动日期','申请时间','抢购是否成功');
		$fields = array('id' => array('type'=>'number'),
				'name'=> array('type'=>'string'),
				'tel'=> array('type'=>'string'),
				'room_id'=> array('type'=>'string'),
				'date' => array('type'=>'string'),
				'apply_time' => array('type'=>'datetime', 'format' => 'Y/m/d h:i:s'),
				'is_error'=>array('type'=>'string')
		          );
		$sheet0 = $objPHPExcel->setActiveSheetIndex(0);
		$rowNumber = 1;
		foreach ($titles as $k => $v) {
			$sheet0->setCellValue(chr(ord('A')+$k).$rowNumber, $v);
		}
		$rowNumber++;
		foreach ( $productOrderGoodsLists as $v ) {
			$fieldIndex = 0;
			foreach ( $fields as $k0 => $v0 ) {
				$cellName = chr(ord('A')+$fieldIndex++).$rowNumber;
				$sheet0->setCellValueExplicit($cellName, $this->getShowData($v[$k0], $v0), $this->getCellFormat($v[$k0]));
				//$sheet0->setCellValue($cellName, );
			}
			$rowNumber++;
		}
		$objPHPExcel->getActiveSheet()->setTitle($date."申请人员信息");
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.date("Ymdhis").'.xls"');
		header('Cache-Control: max-age=0');
		header('Cache-Control: max-age=1'); // IE9时
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	
	/**
	 * 获得单元格格式
	 * @param unknown $fieldCfg
	 * @return string
	 */
	private function getCellFormat( $fieldCfg ) {
		if( $fieldCfg['type'] == 'string' ) {
			return \PHPExcel_Cell_DataType::TYPE_STRING;
		}elseif( $fieldCfg['type'] == 'money' ) {
			return \PHPExcel_Cell_DataType::TYPE_NUMERIC;
		}
		return \PHPExcel_Cell_DataType::TYPE_STRING;
	}
	
	/**
	 * 获得数据展示格式
	 * @param unknown $value
	 * @param unknown $fieldCfg
	 * @return string|unknown
	 */
	private function getShowData($value, $fieldCfg) {
		if( $fieldCfg['type'] == 'datetime' ) {
			return date( $fieldCfg['format'], $value);
		}
		return $value;
	}
	private function search($params){
		
		$condation = array (
				'is_deleted' =>0,
				'is_error'=>0
				
		);
		$params['date'] and $condation['date']=date("Y-m-d",$params['date']);
		$seckillApply = M ( 'SeckillApply' )->where ( $condation )->order ( 'id desc' )->select ();
		foreach ($seckillApply as &$v)
		{
			$v['is_error']='抢购成功';
		}
		
		return $seckillApply;
	}
	
}